<template>
	<view class="container">
		<medical-goods :medicalList="list" pageType="goods"></medical-goods>
		<emptyData :width="empty.width" :height="empty.height" :text="empty.text" :icon="empty.icon" :marginTop="empty.marginTop" v-if="list.length == 0"></emptyData>
	</view>
</template>

<script>
	import medicalGoods from '../../components/medicalGoods/medicalGoods.vue'
	export default {
		components: {
			medicalGoods
		},
		data() {
			return {
				//分页数据
				list:[],
				page: 1,
				category_id: 0,
				empty: {
					width: 135,
					height: 134,
					text: '暂无数据',
					icon: '/image/emptyAddress.png',
					marginTop: 100
				},
			}
		},
		onLoad(e) {
			if(e.category_id) {
				this.category_id = e.category_id
			}

			if(e.category_name) {
				uni.setNavigationBarTitle({
					title: e.category_name
				})
			}
		  this.getList()
		},
		onReachBottom() {
		  this.page++
		  this.getList()
		},
		methods: {
			getList() {
			  this.$api.sendRequest({
			      url: "/api/health/goodslist",
			      data: {
			        page: this.page,
					page_size: 20,
			        category_id: this.category_id,
			      },
			      success: res => {
			        if(res.code == 0) {
						if(this.page == 1) this.list = []
						this.list = [...this.list, ...res.data.list]

						this.list.forEach(item => {
							if(item.goods_label) {
								// 判断里面是否有逗号，如果有就分割成数组
								if(item.goods_label.indexOf(',') != -1) item.goods_label = item.goods_label.split(',')
								else item.goods_label = [item.goods_label]
							}else item.goods_label = []

							if(item.goods_image) {
								var images = item.goods_image.split(',')
								item.goods_image = images[0]
							}

						});
			        } 
			      }
			    })
			},
		}
	}
</script>

<style>

</style>
